﻿var SPListKit = window.SPListKit || {};

SPListKit.RibbonGroupLeafId = "SPListKit";

SPListKit.RibbonResources = {};

SPListKit.RibbonInitOptions = { mergeRibbonGroup: false, localize: false };

SPListKit.overrideRibbonInitFunc = function () {
    if (!window.g_original_ribbonInitFunc1 && typeof (window._ribbonInitFunc1) == "function") {
        g_original_ribbonInitFunc1 = window._ribbonInitFunc1;
        window._ribbonInitFunc1 = function () {
            g_original_ribbonInitFunc1();
            SPListKit.initRibbon();
        };
    }
};

SPListKit.initRibbon = function () {
    if (typeof (_ribbon) == "undefined") return;

    var localize = SPListKit.RibbonInitOptions.localize;
    var mergeGroup = SPListKit.RibbonInitOptions.mergeRibbonGroup;
    if (!localize && !mergeGroup) return;

    var modified = false;

    var newDataExtensions = g_builder.get_ribbonBuildOptions().dataExtensions;
    var groupsChildrenKey = ".Groups._children";
    var controlsChildrenKey = ".Controls._children";
    for (var extensionKey in newDataExtensions) {
        if (SPListKit.endsWith(extensionKey, groupsChildrenKey)) {

            var groups = newDataExtensions[extensionKey];
            if (!localize && groups.length <= 1) continue;

            var controls = null;
            for (var groupIndex = 0; groupIndex < groups.length; groupIndex++) {
                // get duplicated 'SP List Kit' group
                var group = groups[groupIndex];
                if (group.name != "Group") continue;
                if (!SPListKit.endsWith(group.attrs.Id, SPListKit.RibbonGroupLeafId)) continue;
                var controlsExists = false;
                var groupChild = null;
                for (var childIndex = 0; childIndex < group.children.length; childIndex++) {
                    groupChild = group.children[childIndex];
                    if (groupChild.name == "Controls") {
                        controlsExists = (groupChild.children.length > 0);
                        break;
                    }
                }
                if (!controlsExists) continue;

                if (localize) {
                    if (SPListKit.localizeRibbonControls(groupChild.children)) {
                        modified = true;
                    }
                }

                if (!mergeGroup) continue;

                if (!controls) {
                    controls = groupChild.children;
                } else {
                    // merge child controls
                    for (var controlIndex = 0; controlIndex < groupChild.children.length; controlIndex++) {
                        var control = groupChild.children[controlIndex];
                        var sequence = control.attrs.Sequence;
                        if (controls[controls.length - 1].attrs.Sequence < sequence) {
                            controls.push(control);
                        } else if (controls[0].attrs.Sequence > sequence) {
                            controls.unshift(control);
                        } else {
                            for (var insertPos = 1; insertPos < controls.length; insertPos++) {
                                if (controls[insertPos].attrs.Sequence > sequence) {
                                    controls.splice(insertPos, 0, control);
                                    break;
                                }
                            }
                        }
                    }
                    groups.splice(groupIndex--, 1);
                    modified = true;
                }
            }
        } else if (localize && SPListKit.endsWith(extensionKey, controlsChildrenKey)) {
            if (SPListKit.localizeRibbonControls(newDataExtensions[extensionKey])) {
                modified = true;
            }
        }
    }
    if (!modified) return; // already merged, or no duplication

    g_builder.get_ribbonBuildOptions().dataExtensions = newDataExtensions;
    g_builder.$2Y_0 = false;  // This flag is used in buildRibbonAndInitialTab in cui.js. And it will be true in the function.
    g_builder.buildRibbonAndInitialTab(_ribbon.initialTabId);
};

SPListKit.localizeRibbonControls = function (controls) {
    var modified = false;
    for (var controlIndex = 0; controlIndex < controls.length; controlIndex++) {
        var control = controls[controlIndex];
        if (!SPListKit.RibbonResources[control.attrs.Id]) continue;

        var res = SPListKit.RibbonResources[control.attrs.Id];
        if (res.LabelText) {
            control.attrs.LabelText = res.LabelText;
            modified = true;
        }
        if (res.ToolTipTitle) {
            control.attrs.ToolTipTitle = res.ToolTipTitle;
            modified = true;
        }
        if (res.ToolTipDescription) {
            control.attrs.ToolTipDescription = res.ToolTipDescription;
            modified = true;
        }
    }
    return modified;
};

SPListKit.localizeStaticRibbonControls = function () {
    // require jquery
    for (var key in SPListKit.RibbonResources) {
        var control = $("[id|='" + key + "']");
        if (!control.length) continue;
        var label = control.children("span[class$='label']"); // doesn't work if using .find()
        label.html(SPListKit.RibbonResources[key].LabelHtml);
    }
};

SPListKit.endsWith = function (text, subText) {
    return text.indexOf(subText) == (text.length - subText.length);
};
